package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.manager.CarModelManager;
import com.mhc.suzuki.service.CarModelService;
import com.mhc.suzuki.vo.CarModelVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Description: 车型
 * @author: kanon
 * @create: 2017/10/19 15:37
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CarModelServiceImpl implements CarModelService {

    @Autowired
    private CarModelManager carModelManager;

    private List<CarModelVO> carModelVOList;

    @Override
    public List<CarModelVO> getCarModelList(String keyword) {
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("keyword", keyword);
        carModelVOList = carModelManager.selectListByQuery(paramMap);
        return carModelVOList;
    }

    @Override
    public List<CarModelVO> getCarModelListCache(String keyword) {
        return this.getCarModelListByLike(keyword, carModelVOList);
    }

    private List<CarModelVO> getCarModelListByLike(String keyword, List<CarModelVO> carModelVOList) {
        List<CarModelVO> carModelList = new ArrayList<>();
        carModelVOList.parallelStream().filter(carModelVO -> Pattern.compile(keyword, Pattern.CASE_INSENSITIVE).matcher(carModelVO.getBrand() + "" + carModelVO.getSeries()).find()).forEach(carModelVO -> carModelList.add(carModelVO));
        return carModelList;
    }
}
